package org.gdpu.action;

import com.opensymphony.xwork2.ModelDriven;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.gdpu.model.Person;
import org.gdpu.model.Resource;
import org.gdpu.model.Valid;
import org.gdpu.service.PersonService;
import org.gdpu.service.ResourceService;
import org.gdpu.vo.PersonVO;
import org.gdpu.vo.ResourceVO;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-6-4
 * Time: 下午10:13
 * To change this template use File | Settings | File Templates.
 */
@Component("person")
@Scope("prototype")
public class PersonAction extends MyActionSupport implements ModelDriven<PersonVO> {

    private PersonService personService;

    private final PersonVO personVO = new PersonVO();

    private Logger logger = LogManager.getLogger(PersonAction.class.getName());


    @Override
    public String execute() throws Exception {
        HttpSession session = ServletActionContext.getRequest().getSession();
        Person person = (Person) session.getAttribute("person");
        if (person != null) {
            if (personVO.getPassword().equals(person.getPassword())) {
                person.setPassword(personVO.getPasswordConfer());
                personService.update(person);
                logger.trace("User:" + person.getAccout() + " Change Password.");
                personVO.setResultMessage("密码修改成功！");
                return SUCCESS;
            } else {
                personVO.setResultMessage("原密码错误！");
                return ERROR;
            }
        }
        personVO.setResultMessage("请登录！");
        return ERROR;
    }

    public String addStudent() throws Exception {
        if("".equals(personVO.getTeamName())){
            personVO.setResultMessage("请选择学生的组别！");
            return ERROR;
        }

        if("".equals(personVO.getInfo())){
            personVO.setResultMessage("请填写学生信息！");
            return ERROR;
        }
        String info = personService.addStuToTeam(personVO.getInfo(), personVO.getTeamName());

        if ("".equals(info)) {
            personVO.setResultMessage("添加成功!");
        } else {
            personVO.setResultMessage("以下学生已存在：<br/>" + info);
            return ERROR;
        }

        return SUCCESS;
    }

    @Override
    public PersonVO getModel() {
        return personVO;
    }

    @javax.annotation.Resource(name = "personServiceImpl")
    public void setPersonService(PersonService personService) {
        this.personService = personService;
    }
}
